﻿//创建XMLHttpRequest对象      
function createXMLHttpRequest() {
    var obj;
    if (window.XMLHttpRequest) { //Mozilla 浏览器 
        obj = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // IE浏览器 
        try {
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    return obj;
}
//当输入框的内容变化时，调用该函数
function searchSuggest(txtSearch, e) {
    var inputField = document.getElementById(txtSearch);
    
    var suggestText = document.getElementById("search_suggest");

    if (inputField.value.length > 0) {
        var o = createXMLHttpRequest();

        var url = "Server.aspx?searchText=" + escape(inputField.value);

        o.open("GET", url, true);

        o.onreadystatechange = function () {
            //alert('success');
            if (o.readyState == 4) {
                if (o.status == 200) {
                    // alert(o.responseText);
                    var divLeft = e.offsetLeft;
                    var divTop = e.offsetTop;
                    var widthT = e.width;
                    while (e = e.offsetParent) {
                        divLeft += e.offsetLeft;
                        divTop += e.offsetTop;
                    }

                    suggestText.style.left = divLeft + "px";
                    suggestText.style.top = divTop + 20 + "px";
                    suggestText.style.width = 230 + "px";
                    suggestText.style.display = "block";
                    //alert(suggestText.style.width);
                    var sourceText = o.responseText.split("\n");
                    if (sourceText.length > 1) {
                        suggestText.style.display = "";
                        suggestText.innerHTML = "";
                        for (var i = 0; i < sourceText.length - 1; i++) {
                            var s = '<div id="divx' + i + '" onmouseover="javascript:suggestOver(this);"';
                            s += ' onmouseout="javascript:suggestOut(this);" ';
                            s += ' onclick="javascript:setSearch(this.innerHTML);" ';
                            s += ' class="suggest_link">' + sourceText[i] + '</div>';
                            suggestText.innerHTML += s;
                        }
                    }
                    else {
                        suggestText.style.display = "none";
                    }
                }
            }
        };      //指定响应函数 
        o.send(null); // 发送请求 
    }
    else {
        suggestText.style.display = "none";
    }
}

function suggestOver(div_value) {
    div_value.className = "suggest_link_over";
}

function suggestOut(div_value) {
    div_value.className = "suggest_link";
}

function setSearch(obj) {
    document.getElementById('cphInner_txtSearch').value = obj;   //use const
    var div = document.getElementById("search_suggest");
    div.innerHTML = "";
    div.style.display = "none";
}

function tbblur() {
    var div = document.getElementById("search_suggest");
    //div.innerHTML = "";
    div.style.display = "none";
}

